//
//  ZFBBusinessTypeView.m
//  ZFBCollectionDemo
//
//  Created by Apple on 2018/3/25.
//  Copyright © 2018年 techbird. All rights reserved.
//

#import "ZFBBusinessTypeView.h"
#import "Masonry.h"
#import "ZFBBusinessTypeFlowLayout.h"
#import "ZFBBusinessTypeCell.h"

@interface ZFBBusinessTypeView()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic,strong)UICollectionView *collectionView;
@property (nonatomic,strong)UIPageControl *pc;
@end

static NSString *cellid = @"cellid";
@implementation ZFBBusinessTypeView

-(void)awakeFromNib{
    [super awakeFromNib];
    [self setupUI];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}

-(void)setupUI{
    [self makeCollectionView];
    [self makePageControl];
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    NSInteger page = scrollView.contentOffset.x / scrollView.bounds.size.width + 0.499;
    _pc.currentPage = page;
}

- (void)makePageControl{
    UIPageControl *pc = [[UIPageControl alloc]init];
    pc.numberOfPages = 2;
    pc.currentPage = 0;
    pc.pageIndicatorTintColor = [UIColor colorWithWhite:0.7 alpha:1];
    pc.currentPageIndicatorTintColor = [UIColor darkGrayColor];
    [self addSubview: pc];
    
    [pc mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.offset(0);
        make.centerY.equalTo(_collectionView.mas_bottom).offset(-8);
    }];
    
    _pc = pc;
}

- (void)makeCollectionView{
    ZFBBusinessTypeFlowLayout *layout = [[ZFBBusinessTypeFlowLayout alloc]init];
    UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    collectionView.backgroundColor = [UIColor whiteColor];
    [collectionView registerClass:[ZFBBusinessTypeCell class] forCellWithReuseIdentifier:cellid];
    [self addSubview:collectionView];
    
    [collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.offset(0);
        make.height.offset(180);
    }];
    
    collectionView.showsVerticalScrollIndicator = NO;
    collectionView.showsHorizontalScrollIndicator = NO;
    collectionView.pagingEnabled = YES;
    collectionView.bounces = NO;
    _collectionView = collectionView;
}

#pragma mark -UICollectionViewDataSource
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _businessTypeData.count;;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    ZFBBusinessTypeCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellid forIndexPath:indexPath];
    cell.businessType = _businessTypeData[indexPath.item];
    return cell;
}

@end
